<script setup lang="ts" name="SelectUser">
import { authUserSelectAllApi, unallocatedUserApi } from "@/api/system/role";

const { proxy } = getCurrentInstance();

const route = useRoute();

const { sys_normal_disable } = useDict("sys_normal_disable")

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  title: {
    type: String,
    default: false,
  }
})

// 搜索框
const queryUserRef = ref(null);

// 弹窗开关
const open = ref(false);

// 加载
const loading = ref(false);

// 提交加载
const submitLoading = ref(false);

// 用户表数据
const userList = ref<UserRecord[]>([]);

// 选中数组
const ids = ref<number[]>([])

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  userName: { label: "用户名称", visible: true },
  nickName: { label: "用户昵称", visible: true },
  email: { label: "邮箱", visible: true },
  phonenumber: { label: "手机", visible: true },
  status: { label: "状态", visible: true },
  createTime: { label: "创建时间", visible: true },
  userDescribe: { label: "描述", visible: true },
})

// 查询参数
const queryParams = ref<RoleAuthRecord>({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId,
  userName: null,
  phonenumber: null,
});

/** 获取菜单管理详情 */
function getData() {
  open.value = true;
}

/** 查询用户列表 */
function getList() {
  loading.value = true;
  unallocatedUserApi(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    userList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    })
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryUserRef.value.resetFields()
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: UserRecord[]) {
  ids.value = selectionList.map(item => item.userId);
}

/** 提交按钮 */
function submitForm() {
  if (!ids.value.length) {
    proxy.$modal.msgError("请选择要分配的用户");
    return;
  }
  submitLoading.value = true;
  authUserSelectAllApi({
    roleId: route.params.roleId,
    userIds: ids.value.join(',')
  }).then(() => {
    proxy.$modal.msgSuccess("授权成功成功");
    submitLoading.value = false;
    closeDialog(true);
  }).catch(() => {
    submitLoading.value = false;
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
})

getData();
getList();
</script>

<template>

  <!-- 角色用户选项对话框 -->
  <el-dialog :title="title" v-model="open" width="915px" append-to-body :close-on-click-modal="false">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryUserRef" :inline="true" label-width="68px">
        <el-form-item label="用户名称" prop="userName">
          <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" height="400px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="55" prop="colNo" v-if="columns.colNo.visible" />
        <el-table-column label="用户名称" prop="userName" show-overflow-toolti />
        <el-table-column label="用户昵称" prop="nickName" show-overflow-tooltip />
        <el-table-column label="邮箱" prop="email" show-overflow-tooltip />
        <el-table-column label="手机" prop="phonenumber" show-overflow-tooltip min-width="160" />
        <el-table-column label="状态" prop="status" align="center">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="submitLoading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>